<template>
  <div class="container">
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="小智同学"
    ></van-nav-bar>
    <div class="chat-list">
      <div
        class="chat-item"
        v-for="(item, index) in list"
        :key="index"
        :class="{
          left: item.name === 'xz',
          right: item.name === 'me'
        }"
      >
        <template v-if="item.name === 'xz'">
          <!-- 左侧是机器人小智 -->
          <van-image
            fit="cover"
            round
            src="https://img.yzcdn.cn/vant/cat.jpeg"
          />
          <div class="chat-pao">{{ item.msg }}</div>
        </template>
        <template v-if="item.name === 'me'">
          <!-- 右侧是当前用户 -->
          <div class="chat-pao">{{ item.msg }}</div>
          <van-image fit="cover" round :src="photo" />
        </template>
      </div>
    </div>
    <div class="reply-container van-hairline--top">
      <van-field v-model.trim="word" placeholder="说点什么...">
        <span @click="send()" slot="button" style="font-size:12px;color:#999"
          >提交</span
        >
      </van-field>
    </div>
  </div>
</template>

<script>
let globSocket = null;
import { mapState } from "vuex";
import io from "socket.io-client";
export default {
  name: "UserChat",
  data() {
    return {
      word: "",
      // 设计聊天记录
      list: [
        {
          name: "xz",
          msg: "你好，我是小智",
          timestamp: Date.now()
        },
        {
          name: "me",
          msg: "我是编程小王子！",
          timestamp: Date.now()
        },
        {
          name: "xz",
          msg: "你以为会在百度上抄代码，就是程序员了吗？",
          timestamp: Date.now()
        },
        {
          name: "me",
          msg: "不是这样吗？",
          timestamp: Date.now()
        }
      ] // 聊天记录

      // photo:this.$store.state.photo
    };
  },
  computed: {
    ...mapState(["photo", "tokenInfo"])
    //   photo(){
    //       return this.$store.state.photo
    //   }
  },
  created() {
    this.link();
  },
  methods: {
    send() {
      console.log(this.word);
      // 1. 获取参数
      const word = this.word;
      const info = {
        msg: word,
        timestamp: Date.now()
      };
      // 2. 发送给服务器
      globSocket.emit("message", info);
      // 3. 我们的新内容更新视图
      this.list.push({
        name: "me",
        msg: info.msg,
        timestamp: info.timestamp
      });
      // clear word
      this.word = "";
    },
    link() {
      // 创建链接
      const token = this.tokenInfo.token;
      globSocket = io("http://ttapi.research.itcast.cn", {
        query: {
          token: token
        }
      });
      // 监听下是否成功
      globSocket.on("connect", () => {
        this.list.push({
          name: "xz",
          msg: "我准备ok了",
          timestamp: Date.now()
        });
      });
      // 接受服务端主动向我们推送的消息

      globSocket.on("message", obj => {
        this.list.push({
          name: "xz",
          msg: obj.msg,
          timestamp: obj.timestamp
        });
      });
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: "";
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 15px;
    &::before {
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 15px;
    margin-right: 0;
    &::before {
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
